$().ready(function() {
	loadPur();
	loadComplany();
	loadPaytype();
	validateRule();
	loadManagerOper();
	laydate({
		elem : '#payDate'
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/fin/payment/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			appliAmount : {
				required : true
			},
			payDate : {
				required : true
			},
			purId : {
				required : true
			},
			payee : {
				required : true
			},
			paymentComid : {
				required : true
			}
		},
		messages : {
			appliAmount : {
				required : icon + "请输入申请金额"
			},
			payDate : {
				required : icon + "请选择付款时间"
			},
			purId : {
				required : icon + "请选择采购合同"
			},
			payee : {
				required : icon + "请输入收款方"
			},
			paymentComid : {
				required : icon + "请选择付款方"
			}
		}
	})
}
function loadPur(){
	var html = "";
	$.ajax({
		url : '/pur/group/getlistByPayment',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].groupid + '</option>'
			}
			$(".chosen-select-purId").append(html);
			$(".chosen-select-purId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-purId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
				var url ='/pur/group/getgroup/'+params.selected;
				$.ajax({
					url :url,
					data:null,
					type:"get",
					success : function(data) {
						$("input[name='purAmount']").val(Number(data.allPurprice));
						$("input[name='paymentPrice']").val(Number(data.paymentPrice));
						$("input[name='payee']").val(data.supplierName);
						$("input[name='payeeAccount']").val(data.bankAccount);
						$("input[name='bankName']").val(data.openBank);
					}
				});

				layui.use(['form'], function() {
					var layer = layui.layer;
					$("#appliAmount").change(function(){
						var val = $(this).val()
						var purAmount = $("input[name='purAmount']").val()
						var appliAmount = $("input[name='paymentPrice']").val()
						if(val - appliAmount > Number(purAmount)){
							layer.msg('不能大于合同总额');
						}

					})
				})

			});
		}
	});
}
function loadComplany(){
	var html = "";
	$.ajax({
		url : '/system/company/getCompanyList',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-paymentComid").append(html);
			$(".chosen-select-paymentComid").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-paymentComid').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}
function loadPaytype(){
	var html = "";
	$.ajax({
		url : '/itm/paytype/paytypelist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-paymentType").append(html);
			$(".chosen-select-paymentType").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-paymentType').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}
//加载经理员工
function loadManagerOper(){
	var html = "";
	$.ajax({
		url : '/sys/user/getlistbymanager',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].userId + '">' + data[i].name + '</option>'
			}
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select').on('change', function(e, params) {
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

